//
//  HOSearchContactViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 2/28/17.
//  Copyright © 2017 WeiDu. All rights reserved.
//

#import "HOSearchContactViewController.h"
#import "HOContactEntity.h"
#import "HOConstants.h"
#import "HOImageMgr.h"


@interface HOSearchContactViewController ()
{
}
@property(nonatomic, retain)NSString *key;
@end

@implementation HOSearchContactViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	[self initSearchView];
	[self registerKeyboard];
	//	[self initTableViewWithPullUp];
	// Do any additional setup after loading the view.
	//.[self initData];
}

- (void)initData
{
	/*for(int i = 0; i < 10; i ++)
	 {
	 HOContactEntity *entity = [[HOContactEntity alloc] init];
	 entity.companyId = [NSString stringWithFormat:@"0000%d", i];
	 entity.companyName = [NSString stringWithFormat:@"医院名字:%d", i];
	 [searchView.dataList addObject:entity];
	 [entity release];
	 }*/
}



- (void)pullUpToRefresh
{
	HOContactEntity *entity = [searchView.dataList lastObject];
	if(entity)
	{
		[self gotoSearchKey:self.key withMaxId:entity.userId];
	}
	else
	{
		[self gotoSearchKey:self.key withMaxId:1];
	}
}

- (void)pullDownToRefresh
{
	[self gotoSearchKey:self.key withMaxId:1];
}


- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"搜索人员", @"搜索人员")];
}

- (void)gotoSearchKey:(NSString *)key withMaxId:(int)maxId
{
	self.key = key;
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetContactList, kHO_FIELD_Keywords:key,
								  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
}


- (void)gotoSearchKey:(NSString *)key
{
	
	if(key.length < 2)
	{
		[self popupHintView:@"请输入两字或者两字以上关键字搜索"];
		return;
	}
	[self gotoSearchKey:key withMaxId:1];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
	return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 64;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [searchView.dataList count];
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	HOContactEntity *contactEntity = [searchView.dataList objectAtIndex:indexPath.row];
	
	UIImageView *imgView = (UIImageView *)[cell viewWithTag:109];
	if(imgView == nil)
	{
		imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 48, 48)] autorelease];
		imgView.tag = 109;
		[cell.contentView addSubview:imgView];
//		imgView.layer.borderWidth = 1;
	}
	[imgView sd_setImageWithURL:[NSURL URLWithString:contactEntity.headPic] placeholderImage:[[HOImageMgr sharedInstance] getDefaultImage]];

	
	
	UILabel *nameLbl = (UILabel *)[cell viewWithTag:110];
	if(nameLbl == nil)
	{
		nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(68, 8, tableView.frame.size.width - 130, 22)];
		nameLbl.tag = 110;
		nameLbl.font = [UIFont systemFontOfSize:17];
		[cell.contentView addSubview:nameLbl];
		//		nameLbl.layer.borderWidth = 1;
		[nameLbl release];
	}
	nameLbl.text  = [NSString stringWithFormat:@"%@ - %@", contactEntity.userName, contactEntity.departmentName];
	
	UILabel *lbl = (UILabel *)[cell viewWithTag:111];
	if(lbl == nil)
	{
		lbl = [[UILabel alloc] initWithFrame:CGRectMake(68, 34, 100, 22)];
		lbl.tag = 111;
		lbl.textAlignment = NSTextAlignmentRight;
		lbl.font = [UIFont systemFontOfSize:14];
		lbl.textColor = [UIColor grayColor];
//		lbl.layer.borderWidth = 1;
		[cell.contentView addSubview:lbl];
		[lbl release];
	}
	lbl.text  = contactEntity.telephone;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	HOContactEntity *contactEntity = [searchView.dataList objectAtIndex:indexPath.row];
	[self sendCommand:kHO_CMD_SELECDTED_COMPANY data:contactEntity];
	[self.delegate didSelectContact:contactEntity];
	[self goBack];
}
#pragma mark

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if(![response.action isEqualToString:kHO_ACTION_GetContactList])
		return;
	[self.tableViewDataList removeAllObjects];
	NSArray *list = [response.dataNode objectForKey:kHO_FIELD_MemberList];
	for(int i = 0; i < [list count]; i ++)
	{
		NSDictionary *dic = [list objectAtIndex:i];
		HOContactEntity *entity = [[HOContactEntity alloc] init];
		[entity parseDic:dic];
		[searchView.dataList addObject:entity];
		[entity release];
	}
	[searchView reloadData];
	
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

